<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[0].parent.id}`">{{
          goods.categories[0].parent.name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="goods" />
          <GoodsSku @change="change" :goods="goods" />
          <XtxNumbox v-model="num" :max="goods.inventory">数量</XtxNumbox>
          <XtxButton type="primary" style="margin-top: 20px"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <div class="goods-tabs"></div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn></GoodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GootHot :goodsId="goods.id" :type="1"></GootHot>
          <GootHot :goodsId="goods.id" :type="2"></GootHot>
          <GootHot :goodsId="goods.id" :type="3"></GootHot>
        </div>
      </div>
    </div>
  </div>
  <div class="loading" v-else></div>
</template>

<script>
import { useRoute } from 'vue-router'
import GoodsRelevant from './components/goods-relevant'
import { findGoods } from '@/api/product'
import { ref, provide, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GootHot from './components/goot-hot.vue'
import GoodsWarn from './components/goods-warn.vue'

export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsName,
    GoodsSales,
    GoodsSku,
    GoodsTabs,
    GootHot,
    GoodsWarn
  },
  setup () {
    const goods = ref(null)
    const router = useRoute()
    const num = ref(1)
    watch(
      () => router.params.id,
      () => {
        findGoods(router.params.id).then((res) => {
          goods.value = res.result
          console.log(goods.value)
        })
      },
      {
        immediate: true
      }
    )
    const change = (data) => {
      goods.value.inventory = data.inventory
    }

    // 分享给后代组件，可以动态变化
    // 依赖注入       推荐
    provide('goods', goods)
    // 子组件获取的方法
    // inject('goods')
    return { goods, num, change }
  }
}
</script>

<style scoped lang='less'>
.loading {
  width: 1240px;
  height: 600px;
  margin: 50px auto;
  background:#fff url(~@/assets/images/load.gif) no-repeat center;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
